package app

//Api接口Json返回对象
type Rest map[string]interface{}

//新建对象,code=0
func NewRest() Rest {
    return Rest{}.Ok()
}

//消息编码
func (r Rest) Code(code int) Rest {
    r["code"] = code
    return r
}

//消息内容
func (r Rest) Msg(msg string) Rest {
    r["msg"] = msg
    return r
}

//数据对象
func (r Rest) Data(data interface{}) Rest {
    r["data"] = data
    return r
}

//成功消息,设置code=0
func (r Rest) Ok() Rest {
    r.Code(0)
    return r
}

//错误消息,设置code=-1
func (r Rest) Error() Rest {
    r.Code(-1)
    return r
}

func (r Rest) Result(err error) Rest {
    if err != nil {
        r.Code(-1)
        r.Msg(err.Error())
    } else {
        r.Code(0)
    }
    return r
}
